﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Kugar.UI.Web.AdminLTE.PageControls.PageCommon;
using Kugar.UI.Web.AdminLTE.PageControls.Helpers.ButtonExtMethod;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_ButtonList:PageForm_FieldBase
    {
        private List<ButtonConfigBase> _buttons=new List<ButtonConfigBase>();

        public PageForm_ButtonList(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_buttonlistfield");
            _label = "";
        }

        public PageForm_ButtonList Label(string label)
        {
            return this;
        }

        public PageForm_ButtonList AddButton(Action<ButtonConfigBase> configFunc)
        {
            var config=new ButtonConfigBase(ViewContext);
            config.Css("btn");

            configFunc(config);
            
            _buttons.Add(config);

            return this;
        }

        public PageForm_ButtonList AddSubmitButton(Action<ButtonConfigBase> configFunc=null)
        {
            var config=new ButtonConfigBase(ViewContext);
            config.Css("btn");
            config.Type(ButtonTypeEnum.submit).Color(ColorPaletteEnum.success).Text("保存");

            configFunc?.Invoke(config);
            
            _buttons.Add(config);

            return this;
        }

        protected override void RenderContent(TextWriter writer)
        {
            if (_buttons.HasData())
            {
                foreach (var config in _buttons)
                {
                    config.Css("btn-" + config.GetColor.ToStringEx());

                    config.Command?.RenderToElement(config.Attributes);

                    writer.WriteLine($"<button {config.Attributes}>{config.GetText}</button>");
                }
            }
        }
    }
}
